package com.hsq.androidproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.entity.Details;
import com.hsq.androidproject.ui.VideoViewActivity;
import com.hsq.androidproject.utils.glide.GlideRoundTransform;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Assass on 2017/3/19.
 */

public class DetailsAdapter extends BaseAdapter {
    List<Details.VideosBean> mData;
    Context context;

    public DetailsAdapter(List<Details.VideosBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_qzitem, parent, false);
            viewHolder.ll_layout = (LinearLayout) convertView.findViewById(R.id.ll_layout);
            viewHolder.iv_images = (ImageView) convertView.findViewById(R.id.iv_images);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_playCount = (TextView) convertView.findViewById(R.id.tv_playCount);
            viewHolder.tv_danmuCount = (TextView) convertView.findViewById(R.id.tv_danmuCount);
            viewHolder.tv_publishTime = (TextView) convertView.findViewById(R.id.tv_publishTime);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final Details.VideosBean videosBean = mData.get(position);
        String thumb = videosBean.getThumb();
        Glide.with(context)
                .load(thumb)
                .transform(new GlideRoundTransform(context, 3))
                .into(viewHolder.iv_images);

        String title = videosBean.getTitle();
        viewHolder.tv_title.setText(title);

        int danmuCount = videosBean.getDanmuCount();
        viewHolder.tv_danmuCount.setText(danmuCount + "");

        int playCount = videosBean.getPlayCount();
        viewHolder.tv_playCount.setText(playCount + "");

        long publishTime = videosBean.getPublishTime();
        String time = getTimeFromMillisecond(publishTime);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new Date());

        long diff = 0;
        long hours = 0;
        long day = 0;
        long minutes = 0;
        try {
            Date d1 = sdf.parse(date);
            Date d2 = sdf.parse(time);
            diff = d1.getTime() - d2.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            day = days;
            hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (hours > 0) {///hours + ":" + minutes
            viewHolder.tv_publishTime.setText(hours + "小时前");
        } else if (hours == 0) {
            if (minutes > 1) {
                viewHolder.tv_publishTime.setText(minutes + "分钟前");
            } else {
                viewHolder.tv_publishTime.setText("刚刚");
            }
        } else if (hours < 0 && hours > -23) {
            viewHolder.tv_publishTime.setText("昨天");
        } else {
            viewHolder.tv_publishTime.setText(day + "天前");
        }

        /////viewHolder.tv_publishTime.setText(time + "现在的时间：" + date + "=" + hours + ":" + minutes);
        viewHolder.ll_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = videosBean.getSources().get(0).getPage();
                Intent intent = new Intent(context, VideoViewActivity.class);
                intent.putExtra("path", path);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        private TextView tv_title, tv_playCount, tv_danmuCount, tv_publishTime;
        private ImageView iv_images;
        private LinearLayout ll_layout;
    }

    public static String getTimeFromMillisecond(Long millisecond) {		/*毫秒转换为时间*/
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date(millisecond);
        String timeStr = simpleDateFormat.format(date);
        return timeStr;
    }
}
